//
//  TDChooseView.m
//  TotalDemo
//
//  Created by gujian on 16/6/13.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TDChooseView.h"

@interface TDChooseView()

@property (nonatomic, assign) CGPoint beginPos;
@property (nonatomic, assign) CGPoint originPos;
@property (nonatomic, assign) CGPoint currentPos;

@property (nonatomic, weak) UIView *rightTopView;

@end

@implementation TDChooseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor randomColor];
        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.layer.borderWidth = 2.0f;
        [self setupSubViews];
        [self addGesture];
    }
    
    return self;
}

- (void)setupSubViews
{
    CGFloat viewWidth = 10.0;
    UIView *rightTopView = [[UIView alloc] initWithFrame:CGRectMake(self.width-viewWidth/2.0-1.0, -viewWidth/2.0+1.0, viewWidth, viewWidth)];
    rightTopView.userInteractionEnabled = YES;
    rightTopView.backgroundColor = [UIColor whiteColor];
    rightTopView.layer.cornerRadius = viewWidth/2.0;
    rightTopView.layer.masksToBounds = YES;
    [self addSubview:rightTopView];
    _rightTopView = rightTopView;
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightTopViewGesture:)];
    [rightTopView addGestureRecognizer:panGesture];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat viewWidth = 10.0;
    _rightTopView.frame = CGRectMake(self.width-viewWidth/2.0-1.0, -viewWidth/2.0+1.0, viewWidth, viewWidth);
}

- (void)addGesture
{
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self addGestureRecognizer:panGesture];
}


- (void)handleGesture:(UIGestureRecognizer *)gesture
{
    CGPoint pos = [gesture locationInView:self.superview];

    
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        _beginPos = [gesture locationInView:self];
        self.layer.anchorPoint  = CGPointMake(_beginPos.x/self.width, _beginPos.y/self.height);
    }else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
//        self.layer.anchorPoint = CGPointMake(0.5, 0.5);
    }
    
    self.center = pos;

   
}

- (void)handleRightTopViewGesture:(UIGestureRecognizer *)gesture
{
    
    
    CGPoint pos = [gesture locationInView:self.superview];

    _originPos = _currentPos;

    _currentPos = pos;
    if (CGPointEqualToPoint(_originPos, CGPointZero)) {
        return;
    }
    
    CGFloat delta = MAX(_currentPos.x - _originPos.x, fabs(_currentPos.y - _originPos.y));
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        _currentPos = CGPointZero;
    }
    
    LotLog(@"delta:%@",@(delta));
    if ((_currentPos.x - _originPos.x) >= 0) {
        self.top -= delta;
        self.width += delta;
        self.height += delta;
    }else
    {
        self.top += delta;
        self.width -= delta;
        self.height -= delta;
    }

    
    [self layoutIfNeeded];
    
}

@end
